﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using System;

public class FItemListMediator : MyWindow {
    GList itemList;
    private Action<int> _loadItem;
    public Action<int> loadItem{
        set {
            _loadItem = value;
            contentPane.GetChild("ReadGrou").visible = true;
            contentPane.GetChild("OperaGrou").visible = false;
        }
        get {return _loadItem;}
    }
    protected override void OnInit()
    {
        base.OnInit();
        this.Center();//居中
        
    }

    public FItemListMediator(OneDelegate cb){
        this.contentPane = UIPackage.CreateObject("ItemPackage", "ItemList").asCom;

        itemList = contentPane.GetChild("List").asList;
        contentPane.GetChild("saveBtn").asButton.onClick.Add(() => {
            SaveList();
            int id = ItemManager.Instance.GetRepeatId();
            if (id != -1) {
                FUIMain.TipPanel(string.Format("列表内含重复元素：{0}",id));
                return;
            }
            ItemManager.Instance.SaveList();
            FUIMain.TipPanel("保存成功！");

        });
        contentPane.GetChild("loadBtn").asButton.onClick.Add(() =>
        {
            if (itemList.selectedIndex == -1)
            {
                FUIMain.TipPanel("请选择要读取的杂物！");
                return;
            }

            if (loadItem != null) {
                loadItem(itemList.selectedIndex);
                FUIMain.ClosePanel(GMConfig.PanelType.ItemList);
            }
        });
        contentPane.GetChild("removeBtn").asButton.onClick.Add(() =>
        {
            if (itemList.selectedIndex == -1)
            {
                FUIMain.TipPanel("请选择要删除的杂物！");
                return;
            }
            FUIMain.TipPanel("是否确定删除本条数据？", (status) =>
            {
                if ((int)status == 1)
                {
                    ItemManager.Instance.RemoveItem(itemList.selectedIndex);
                }
            });
            itemList.numItems--;
        });
        contentPane.GetChild("returnBtn").asButton.onClick.Add(() =>
        {
            FUIMain.ClosePanel(GMConfig.PanelType.ItemList);
        });
        contentPane.GetChild("closeBtn").asButton.onClick.Add(() =>
        {
            if (cb != null)
            {
                FUIMain.ClosePanel(GMConfig.PanelType.ItemList);
            }
        });
        contentPane.GetChild("createBtn").asButton.onClick.Add(() =>
        {
            ItemManager.Instance.InsertItem(-1);
            itemList.numItems++;
        });
        contentPane.GetChild("insertBtn").asButton.onClick.Add(() =>
       {
           if (itemList.selectedIndex == -1)
           {
               FUIMain.TipPanel("请选择要插入的杂物！");
               return;
           }
           SaveList();
           ItemManager.Instance.SaveList();
           ItemManager.Instance.InsertItem(itemList.selectedIndex + 1);
           itemList.numItems++;
       });
        itemList.itemRenderer = (int index, GObject obj) => {
            GButton item = obj.asButton;
            item.GetChild("imgTextField").asTextInput.onClick.Add(() => {
                FSelectListMediator _selectImgPanel = (FSelectListMediator)FUIMain.ShowPanel(GMConfig.PanelType.SelecImgtPanel);
                _selectImgPanel.InitData("ItemImgPackage", PackageItemType.Image, (object imgUrl) => {
                    item.GetChild("imgTextField").asTextInput.text = imgUrl.ToString();
                });
            });
            if (index >= ItemManager.Instance._itemList.Count) return;
            Item data = ItemManager.Instance._itemList[index];
            int id = data.id == 0 ? index : data.id;
            item.GetChild("IdTextField").asTextInput.text = id.ToString();
            item.GetChild("nameTextField").asTextInput.text = data.name;
            item.GetChild("imgTextField").asTextInput.text = data.icon;
            item.GetChild("priceTextField").asTextInput.text = data.price.ToString();
            item.GetChild("descTextField").asTextInput.text = data.desc;
            item.GetChild("getTextField").asTextInput.text = data.getPath.ToString();
            item.GetChild("typeTextField").asTextInput.text = data.itemType.ToString();
            item.GetChild("limitTextField").asTextInput.text = data.limitTime;
        };
        itemList.numItems = ItemManager.Instance._itemList.Count;
    }

    void SaveList(){
        for (int i = 0; i < itemList.numItems; i++)
        {
            GButton btn = itemList.GetChildAt(i).asButton;
            int id = 0, price = 0, getPath = 0, type = 0;

            int.TryParse(btn.GetChild("IdTextField").asTextInput.text, out id);
            int.TryParse(btn.GetChild("priceTextField").asTextInput.text, out price);
            int.TryParse(btn.GetChild("getTextField").asTextInput.text, out getPath);
            int.TryParse(btn.GetChild("typeTextField").asTextInput.text, out type);

            string name = btn.GetChild("nameTextField").asTextInput.text;
            string icon = btn.GetChild("imgTextField").asTextInput.text;
            string desc = btn.GetChild("descTextField").asTextInput.text;
            string limit = btn.GetChild("limitTextField").asTextInput.text;

            Item item = new Item(id, name, icon, price, desc, getPath, type, limit);
            ItemManager.Instance.ReplaceItem(i, item.DeepClone());
        }    
    }

    protected override void OnShown(){
        base.OnShown();
        if (loadItem == null) {
            contentPane.GetChild("ReadGrou").visible = false;
            contentPane.GetChild("OperaGrou").visible = true;
        }
    }
    protected override void OnHide(){
        base.OnHide();
        // contentPane.GetChild("saveBtn").asButton.FireClick(false);
    }
}
